package com.xcity.db.entity;

import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.game.common.Cause;
import com.xcity.game.land.LandBuyTemplate;
import com.xcity.game.land.LandTemplate;
import com.xcity.game.player.cache.PlayerCacheService;
import com.xcity.pomelo.push.msg.LandBeAcquiredPush;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.game.time.TimeUpdater;

public class Land {

	private int id;
	private long ownerId;
	private int level;
	private Date lastLevelUpTime;
	private Date lastTakeMoneyTime;
	
	private LandTemplate template;
	
	public Land() {}
	
	public Land(int id, Player owner) {
		this.setId(id);
		this.ownerId = owner.getId();
		this.level = 1;
		this.lastLevelUpTime = new Date();
		this.lastTakeMoneyTime = new Date();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		if (id != this.id) {
			this.id = id;
			template = App.getApp().getServiceManager().get(TemplateService.class).get(LandTemplate.class, id);
		}
	}

	public long getOwnerId() {
		return ownerId;
	}

	public void setOwnerId(long ownerId) {
		this.ownerId = ownerId;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public Date getLastLevelUpTime() {
		return lastLevelUpTime;
	}

	public void setLastLevelUpTime(Date lastLevelUpTime) {
		this.lastLevelUpTime = lastLevelUpTime;
	}

	public Date getLastTakeMoneyTime() {
		return lastTakeMoneyTime;
	}

	public void setLastTakeMoneyTime(Date lastTakeMoneyTime) {
		this.lastTakeMoneyTime = lastTakeMoneyTime;
	}

	public LandTemplate getTemplate() {
		return template;
	}
	
	public long calcMoney() {
		if (level <= 0 || lastTakeMoneyTime == null) {
			return 0L;
		}
		LandBuyTemplate temp = template.getBuyTemplate(level);
		long elapse = TimeUpdater.getInstance().now() - lastTakeMoneyTime.getTime();
		long money = temp.getIncome() * elapse / 60000;
		return money > temp.getMaxIncome() ? temp.getMaxIncome() : money;
	}
	
	@JSONField(serialize = false)
	public int getCD() {
		if (level <= 0 || lastLevelUpTime == null) {
			return 0;
		}
		LandBuyTemplate temp = template.getBuyTemplate(level);
		long cd = (temp.getCd() * 1000 + lastLevelUpTime.getTime() - TimeUpdater.getInstance().now());
		return (int) (cd > 0 ? cd : 0);
	}
	
	public boolean hasOwner() {
		return ownerId != 0;
	}
	
	public void recycle() {
		changeOwner(null);
	}
	
	public void changeOwner(Player newOwner) {
		if (ownerId != 0) { // 结算之前占有玩家
			long money = calcMoney();
//			Player oldOwner = App.getApp().getServiceManager().get(ActorService.class).find(ownerId);
			Player oldOwner = App.getApp().getServiceManager().get(PlayerCacheService.class).find(ownerId);
			if (oldOwner != null) {
				oldOwner.addGold(money, Cause.LAND_INCOME);
				oldOwner.fireFixedAssetsChanged();
				if (oldOwner.isOnline() && newOwner != null) {
					oldOwner.send(new LandBeAcquiredPush(getTemplate().getName(), newOwner.getName(), oldOwner));
				}
			}
		}
		
		if (lastLevelUpTime == null) {
			lastLevelUpTime = new Date();
		}
		if (lastTakeMoneyTime == null) {
			lastTakeMoneyTime = new Date();
		}
		long now = TimeUpdater.getInstance().now();
		lastTakeMoneyTime.setTime(now);
		
		if (newOwner != null) {
			lastLevelUpTime.setTime(now);
			if (++level > template.getBuyTemplates().size()) {
				level = 1;
			}
			ownerId = newOwner.getId();
			newOwner.fireFixedAssetsChanged();
		} else {
			ownerId = 0L;
		}
	}
	
}
